<?php
class student extends MY_Controller {
	
	function index(){
		$data['title'] = 'Admin';
		$data['school_data'] = $this->school_data;
		
		$this->template->set_body(NULL, $data, TRUE);
		$this->template->load_template('default', $data);
		
	}
	
	function profile($id = FALSE , $cancel = FALSE, $system_message = ''){
	
		if($cancel == md5(strtolower('cancel'))){
			$this->session->set_userdata('checkbox_checked',FALSE);
		}

		if($id == FALSE){
			$id = $this->session->userdata('userid');
		}
		$data['system_message'] = $system_message;
		$data['title'] = 'Edit Student Profile';
		$this->load->helper('data_format');
		
		$this->load->model('M_student');
		$data['db_entries'] = $this->M_student->get_profile($id);
		$data['school_data'] = $this->school_data;
		
		$this->template->set_body('page-blocks/page-module/vStudentEditProfile', $data, TRUE);
		$this->template->load_template('default', $data);
			
	}
	// POST actions edit student
	public function post_edit_student_profile(){

		$user_id = $this->input->post('user_id',TRUE);
		$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
		// Form validation rules can be found in config/form_validation
		if($this->form_validation->run('update_student_data') === FALSE){
			$message = '<div class="error">Some fields have incorrect values</div>';
			$this->session->set_userdata('checkbox_checked',TRUE);
			$this->profile($user_id,'',$message);
		}else{
			$this->load->model('M_student');
			
			$user_d = $this->input->post('user_id',TRUE);
			$input['lastname'] = $this->input->post('lastname',TRUE);
			$input['firstname'] = $this->input->post('firstname',TRUE);
			$input['middlename'] = $this->input->post('middlename',TRUE);
			$input['gender'] = $this->input->post('gender',TRUE);
			$input['civilstatus'] = $this->input->post('civilstatus',TRUE);
			$input['dob'] = $this->input->post('dob',TRUE);
			$input['pob'] = $this->input->post('pob',TRUE);
			$input['age'] = $this->input->post('age',TRUE);
			$input['disability'] = $this->input->post('disability',TRUE);
			$input['nationality'] = $this->input->post('nationality',TRUE);
			$input['religion'] = $this->input->post('religion',TRUE);
			$input['contactnumber'] = $this->input->post('contactnumber',TRUE);
			$input['email'] = $this->input->post('email',TRUE);
			$input['presentaddress'] = $this->input->post('presentaddress',TRUE);
			$input['fathername'] = $this->input->post('fathername',TRUE);
			$input['fatheroccupation'] = $this->input->post('fatheroccupation',TRUE);
			$input['fathercontact'] = $this->input->post('fathercontact',TRUE);
			$input['mothername'] = $this->input->post('mothername',TRUE);
			$input['motheroccupation'] = $this->input->post('motheroccupation',TRUE);
			$input['mothercontact'] = $this->input->post('mothercontact',TRUE);
			$input['parentaddress'] = $this->input->post('parentaddress',TRUE);
			$input['guardianname'] = $this->input->post('guardianname',TRUE);
			$input['guardiancontact'] = $this->input->post('guardiancontact',TRUE);
			$input['guardianaddress'] = $this->input->post('guardianaddress',TRUE);
			$input['eschoolname'] = $this->input->post('eschoolname',TRUE);
			$input['eschooladdress'] = $this->input->post('eschooladdress',TRUE);
			$input['eyeargraduate'] = $this->input->post('eyeargraduate',TRUE);
			$input['sschoolname'] = $this->input->post('sschoolname',TRUE);
			$input['sschooladdress'] = $this->input->post('sschooladdress',TRUE);
			$input['syeargraduate'] = $this->input->post('syeargraduate',TRUE);
			$input['vschoolname'] = $this->input->post('vschoolname',TRUE);
			$input['vschooladdress'] = $this->input->post('vschooladdress',TRUE);
			$input['vyeargraduate'] = $this->input->post('vyeargraduate',TRUE);
			$input['tschoolname'] = $this->input->post('tschoolname',TRUE);
			$input['tschooladdress'] = $this->input->post('tschooladdress',TRUE);
			$input['tyeargraduate'] = $this->input->post('tyeargraduate',TRUE);
			$input['oschoolname'] = $this->input->post('oschoolname',TRUE);
			$input['oschooladdress'] = $this->input->post('oschooladdress',TRUE);
			$input['oyeargraduate'] = $this->input->post('oyeargraduate',TRUE);
			$input['updated'] =  date('Y-m-d H:i ');
			

			if($this->M_student->update_stud_profile($input,$user_id) == FALSE){
				$message = '<div class="error">An erro was encountered while updating data</div>';
				$this->profile($user_id,FALSE,$message);
			}else{
				$success = array(
					'type' => 'success',
					'msg' => 'update was successfull'
				);
				$this->session->set_userdata('checkbox_checked',FALSE);
				$message = '<div class="success">Update Saved</div>';
				$this->profile($user_id,FALSE,$message);
			}
		}
	}
	
	function select_subjects(){
		$data['title'] = 'Select Subjects';
		
		$this->load->model('M_student');
		$this->load->model('M_course');
		
		$student = $this->M_student->get_student_status($this->session->userdata('id_no'));
		$data['school_data'] = $this->school_data;
		
		$this->template->set_body('page-blocks/page-module/vStudentEditProfile', $data, TRUE);
				
		/*
		$data['db_entries2'] = $this->M_course->get_selected_subjects($this->session->userdata('id_no'));
		$data['db_entries'] = $this->M_course->get_all_available_subject($data['db_entries2']);
		
		$view['content'] = $this->load->view('page-blocks/page-module/vMenu', '', TRUE);
		$view['content'] = $view['content'].$this->load->view('page-blocks/page-module/vStudentSelectSubjects', $data, TRUE);	
		*/
		
		if($student->enrollmentstatus == 'approved'){
			$this->template->set_body('page-blocks/page-module/vStudentApprovedSubjectsAndFees', $data, TRUE);
			
		}
		else if($student->enrollmentstatus == 'pending') {
			$data['db_entries'] = $this->M_course->get_selected_subjects($this->session->userdata('id_no'), TRUE);
			$this->template->set_body('page-blocks/page-module/vStudentPendingSubjects', $data, TRUE);
		}
		else {
			$data['db_entries2'] = $this->M_course->get_selected_subjects($this->session->userdata('id_no'));
			$data['db_entries'] = $this->M_course->get_all_available_subject($data['db_entries2']);
			$this->template->set_body('page-blocks/page-module/vStudentSelectSubjects', $data, TRUE);
			
		}
		
		$this->template->load_template('default', $data);

	
	}
	
	function add_subjects(){
		$this->load->model('M_course');
		$this->load->model('M_school');
		$subjects = $this->input->post('selectedsubjects');
		$current_semester = $this->M_course->get_current_semester();
		$current_year = $this->M_school->get_current_sy();

		if(!empty($subjects))
			$this->M_course->add_student_subject($subjects, $this->session->userdata('id_no'), $current_semester->id, $current_year->sy_id);
		
		redirect('student/select_subjects');
	}
	
	function remove_subjects(){
		$this->load->model('M_course');
		$subjects = $this->input->post('addedcourse');
			
		if(!empty($subjects))
			$this->M_course->remove_student_subject($subjects, $this->session->userdata('id_no'));
		
		redirect('student/select_subjects');
		
	}
	
	function enroll(){
		$this->load->model('M_student');	
		if($this->M_student->enroll_student($this->session->userdata('id_no')))
			redirect('student/add_subjects');
	}
}

?>